<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * Member model
 * model สำหรับ member management
 * @package Member
 * @subpackage Model
 * @category Member
 */
class Member_m extends MY_Model {

    function __construct() {

        parent::__construct();
    }

    private $system_table = 'mem_system';
    private $profile_table = 'mem_profile';

    /**
     * Get simple All User
     * เรียก ข้อมูลเบื้องต้นของ user
     * @return array id,username,email,memgroup,activated
     */
    function getSimpleAllUser() {
        $s = sprintf('SELECT id,username,email,memgroup,activated FROM %s ORDER BY id DESC'
                ,$this->system_table);
        return $this->multiplerow($s);
    }

    /**
     * Get all profile from user id
     * เรียกข้อมูลทั้งหมดของสมาชิก โดยใช้ User id
     * @param int userid
     * @return array user profile
     */
    function getUserId($userid) {
        $this->db->from($this->system_table);
        $this->db->join($this->profile_table, $this->profile_table . '.user_id=' . $this->system_table . '.id');
        $this->db->where($this->system_table . '.id', $userid);
        $query = $this->db->get();
        if ($query->num_rows() == 1)
            return $query->row();
        return NULL;
    }

    /**
     * Ban user
     * Ban user พร้อมเหตุผล
     * @param int userid
     * @param string เหตุผล
     */
    function ban_user($userid, $reason) {
        $data = array(
            'banned' => 1,
            'ban_reason' => $reason
        );
        $this->db->where('id', $userid);
        $this->db->update($this->system_table, $data);
    }

    /**
     * Unban user
     * @param int userid
     */
    function unban_user($userid) {
        $this->db->set('banned', null);
        $this->db->set('ban_reason', null);
        $this->db->update($this->system_table);
    }

    /**
     * Delete user record
     * ลบ user
     *
     * @param	int userid
     * @return	bool
     */
    function delete_user($user_id) {
        $this->db->where('id', $user_id);
        $this->db->where('memgroup !=', 120);
        $this->db->delete($this->system_table);

        mysql_query('OPTIMIZE '.$this->system_table);

        $this->delete_profile($user_id);
        return TRUE;
    }

    /**
     * Delete user profile
     * ลบ profile ของ user ออก
     * @param	int userid
     * @return	void
     */
    private function delete_profile($user_id) {
        $this->db->where('user_id', $user_id);
        $this->db->delete($this->profile_table);
        mysql_query('OPTIMIZE '.$this->profile_table);
    }
    /**
     * Activate User
     * @param int $userid
     */
    function activate_user($userid) {
        $this->db->set('activated', 1);
        $this->db->set('new_email_key', NULL);
        $this->db->where('id', $userid);
        $this->db->update($this->system_table);
        //ถ้ายังไม่มี profile ก็สร้างใหม่ด้วย
        $this->db->select('id');
        $this->db->where('user_id',$userid);
        $res = $this->db->get($this->profile_table);
        if($res->num_rows()<1){
            $this->create_profile($userid);
        }
        return TRUE;
    }

    /**
     * Get user record by login or id
     * เรียก user row จากการ login
     *
     * @param	string username, email, id
     * @return	object
     */
    function get_user_by_login($login) {
        $s = sprintf('SELECT * FROM %s WHERE LOWER(username) LIKE \'%s\' OR LOWER(email) LIKE \'%s\' OR id LIKE %d'
                ,$this->system_table
                ,strtolower($login)
                ,strtolower($login)
                ,intval($login));
        return $this->multiplerow($s);
    }

    function updateMemberProfile($array,$userid){
        $this->db->where('user_id',$userid);
        $this->db->update($this->profile_table,$array);
//        echo $this->db->last_query(); die();
    }

    function getMemberGroup($memgroup){
        $this->db->select('id,username,email,memgroup,activated', FALSE);
        $this->db->order_by('id', 'desc');
        $this->db->where('memgroup',$memgroup);
        $res = $this->db->get($this->system_table);
        if($res->num_rows()>0)
                return $res;
        return NULL;
    }


    /**
     * Create an empty profile for a new user
     * สร้าง profile เปล่าๆสำหรับ user ใหม่
     *
     * @param	int userid
     * @return	bool
     */
    private function create_profile($user_id) {
        $this->db->set('user_id', $user_id);
        return $this->db->insert($this->profile_table);
    }

    /**
     * นับจำนวนสมาชิกทั้งหมด
     *
     * @return int จำนวนสมาชิกทั้งหมด (รวมทุกประเภท)
     */
    function countAllMem(){
        return $this->db->count_all($this->system_table);
    }
    /**
     * นับจำนวนสมาชิกที่ถูก ban
     * @return int สมาชิกที่ถูกban
     */
    function countBanMem(){
        $this->db->where('banned',1,false);
        return $this->db->count_all_results($this->system_table);
    }
    /**
     * นับสมาชิกที่ไม่ active
     * @return int สมาชิกที่ยังไม่ active
     */
    function countNotactiveMem(){
        $this->db->where('activated',0,false);
        return $this->db->count_all_results($this->system_table);
    }

}
